import * as TYPES from '../action-types'

// {
// 	code:0,
// 	codeText:'',//数据状态描述信息
// 	limit:10,//每页显示数量
// 	page:1,//当前页码
// 	pageNum:10,//总页数
// 	total:100 //总数据量
// 	list:[
// 	{
// 		id:1,
// 		task:'xxx' ,//任务信息
// 		state: 1, //1-未完成 2-已完成]
// 		time: '2019-01-01 10:00:00', //预计完成时间
// 		complete: '2019-01-01 10:00:00', //真实完成时间
// 	},
// 	...
// ]
// }
// 模拟异步请求
export const mockQueryAllList = () => {
	return new Promise((resolve) => {
		setTimeout(() => {
			// 生成随机任务列表
			const list = Array.from({ length: 100 }, (_, index) => {
				const state = Math.random() > 0.5 ? 1 : 2;
				const time = new Date(Date.now() + Math.random() * 7 * 24 * 60 * 60 * 1000)
					.toISOString()
					.slice(0, 19)
					.replace('T', ' ');
				const complete = state === 2 ? time : '';

				return {
					id: index + 1,
					task: `任务${index + 1}`,
					state,
					time,
					complete
				};
			});

			resolve({
				code: 0,
				codeText: '获取成功',
				limit: 10,
				page: 1,
				pageNum: Math.ceil(list.length / 10),
				total: list.length,
				list
			});
		}, 1000);
	});
}

const taskActions = {
	queryAllList: async () => {
		const result = await mockQueryAllList();
		return {
			type: TYPES.TASK_LIST,
			list: result.list,
		}
	},
	deleteTaskById: (id) => {
		return {
			type: TYPES.TASK_REMOVE,
			id,
		}
	},
	updateTaskById: (id) => {
		return {
			type: TYPES.TASK_UPDATE,
			id,
		}
	},
	addTask: (task) => {
		return {
			type: TYPES.TASK_ADD,
			task,
		}
	}
}

export default taskActions